/***************************************************************************
 *   Copyright (C) 2007 by ipQalc team                                     *
 *   milambert@c-webhosting.org                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QtGui>
#include "maxsubnet.h"
//#include "adrfct.h"

MaxSubnetDialog::MaxSubnetDialog(QWidget *parent) : QWidget(parent){
    //QGroupBox * SubnetGroup = new QGroupBox(tr("Max Subnet"), this);
    QVBoxLayout * layout = new QVBoxLayout;

    // formulaire
    QLabel *ipLabel = new QLabel(tr("IP network"));
    ipEdit = new QLineEdit;
    ipEdit->setInputMask("999.999.999.999; ");

    QLabel *maxClLabel = new QLabel(tr("Max Client"));
    maxClient = new QLineEdit;

    QLabel *idClLabel = new QLabel(tr("n Host"));
    clientId = new QLineEdit;

    QLabel *idSnLabel = new QLabel(tr("n subnet"));
    subnetId = new QLineEdit;

    // set and reset button and signals
    QPushButton * set = new QPushButton(tr("Get Info"));
    QPushButton * reset = new QPushButton(tr("Reset"));

    connect(set, SIGNAL(clicked()), this, SLOT(getInfo()));
    connect(reset, SIGNAL(clicked()), this, SLOT(reset()));

    // results
    QLabel *ipResLabel = new QLabel(tr("IP host"));
    ipResult = new QLabel;
    ipResult->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    ipResult->setMinimumSize(200, 22);

    QLabel *maskResLabel = new QLabel(tr("Mask"));
    maskResult = new QLabel;
    maskResult->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    maskResult->setMinimumHeight(22);

    QLabel *bcResLabel = new QLabel(tr("broadcast"));
    bcResult = new QLabel;
    bcResult->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    bcResult->setMinimumHeight(22);

    QLabel *subResLabel = new QLabel(tr("Subnet"));
    subnetResult = new QLabel;
    subnetResult->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    subnetResult->setMinimumHeight(22);

    QLabel *nbClLabel = new QLabel(tr("nb Host"));
    nbHost = new QLabel;
    nbHost->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
    nbHost->setMinimumHeight(22);

    // formulaire layout
    QGridLayout *editLayout = new QGridLayout;
    editLayout->addWidget(ipLabel, 0, 0);
    editLayout->addWidget(ipEdit, 0, 1);
    editLayout->addWidget(maxClLabel, 0, 2);
    editLayout->addWidget(maxClient, 0, 3);
    editLayout->addWidget(idClLabel, 1, 0);
    editLayout->addWidget(clientId, 1, 1);
    editLayout->addWidget(idSnLabel, 1, 2);
    editLayout->addWidget(subnetId, 1, 3);
    editLayout->addWidget(set, 0, 4);
    editLayout->addWidget(reset, 1, 4);

    // result layout
    QGridLayout *resultLayout = new QGridLayout;
    resultLayout->addWidget(ipResLabel, 0, 0);
    resultLayout->addWidget(ipResult, 0, 1);
    resultLayout->addWidget(maskResLabel, 1, 0);
    resultLayout->addWidget(maskResult, 1, 1);
    resultLayout->addWidget(subResLabel, 2, 0);
    resultLayout->addWidget(subnetResult, 2, 1);
    resultLayout->addWidget(bcResLabel, 3, 0);
    resultLayout->addWidget(bcResult, 3, 1);
    resultLayout->addWidget(nbClLabel, 4, 0);
    resultLayout->addWidget(nbHost, 4, 1);
    resultLayout->setRowStretch(5, 1);
    resultLayout->setColumnStretch(2, 1);

    // layout
    layout->addLayout(editLayout);
    layout->addLayout(resultLayout);
    //SubnetGroup->setLayout(layout);
    this->setLayout(layout);
}

void MaxSubnetDialog::getInfo(){

    ipAdress = new IpAdress(ipEdit->text());

    if (ipAdress->isValid()){
        ipResult->setText (ipAdress->getIpAdress(clientId->text(), subnetId->text(), maxClient->text(), false));
        maskResult->setText (ipAdress->getMask(maxClient->text(), false));
        bcResult->setText (ipAdress->getBroadcast(subnetId->text(), maxClient->text(), false));
        subnetResult->setText(ipAdress->getIpAdress("0", subnetId->text(), maxClient->text(), false));
    } else
        QMessageBox::warning(this, tr("Error"), tr("Invalid ip adress"));
}

void MaxSubnetDialog::reset(){
    ipResult->clear();
    subnetResult->clear();
    maskResult->clear();
    bcResult->clear();
    nbHost->clear();
}

MaxSubnetDialog::~MaxSubnetDialog(){}
